package com.qf.springbootdemo03.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Collection;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/25
 **/
//@Service
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //分配角色，必须给，不能是null
        // GrantedAuthority-->授权    Granted--> 授予   Authority --> 权力
        Collection<? extends GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(""); // "" : 没有角色

        //传入得密码会跟前台输入得密码进行比对，这是在构造User对象时需要去认证的过程
        //User ： 就是认证后的对象
        return new User("tom", passwordEncoder.encode("tom"), authorities);
    }
}
